package engine;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import engine.dataobjects.MailItemInstance;
import engine.services.IDataResource;





public class Setup {

	public ApplicationContext context;
	
	static {
		String userDir;
		if (System.getProperty("user.dir") == null) {
			userDir  =  Setup.class.getClassLoader().getResource("").getPath().replace("\\", "/");
		} else {
			userDir  = System.getProperty("user.dir").replace("\\", "/");			
		}
		userDir = userDir+"/src/test/resources";
		 System.setProperty("user.dir", userDir);
		System.setProperty("log4j.configuration","file:/"+userDir+"/META-INF/log4j.xml");					
	}
	
	public Setup() {
		this("file:src/test/resources/META-INF/testContext.xml", null);
	}

	public Setup(String contextFile) {
		this("file:src/test/resources/META-INF/"+contextFile, new String[] {});
	}
	
	public Setup(String contextFile, String[] args) {
		context = new FileSystemXmlApplicationContext(contextFile);
		List<MailItemInstance<?>> items = ((IDataResource)context.getBean("configResource")).getActiveMailItemInstances();
		((Engine)context.getBean("engine")).initMailItems(items);
	}
	

}
